﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using webnew.Common;

namespace webnew
{
    public partial class ResetPassWord : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(Request.QueryString["MsgType"].ToString()=="0" )
                lblMsg.Text="初次登陆,请用户修改密码."; 
            else if (Request.QueryString["MsgType"].ToString()=="1" )
                lblMsg.Text = "该用户已超过三个月未修改密码,请更改密码,再登陆."; 
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Model.Sys_UserInfo info = new webnew.Model.Sys_UserInfo();
            if (txtUid.Text == string.Empty)
            {
                Jscript.Alert(this, "请输入用户名！");
                return;
            }
            else if (DataValid.CheckNumber(4, txtUid.Text.Trim()))
            {
                Model.View_UserInfo ViewInfo = new webnew.Model.View_UserInfo();
                ViewInfo = new BLL.View_User().GetStaffNOModel(txtUid.Text);
                info = new BLL.Sys_User().GetLogModel(ViewInfo.Loginid);
                if (ViewInfo.StaffNO != txtUid.Text)
                {
                    Jscript.Alert(this, "不存在该员工编号！");
                    return;
                }
            }
            else
            {
                info = new BLL.Sys_User().GetLogModel(txtUid.Text.Trim());
                if (info.Loginid != txtUid.Text)
                {
                    Jscript.Alert(this, "不存在该用户名！");
                    return;
                }
            }
            if (info.password != CommonHelper.MyMd5(txtOldPwd.Text))
            {
                Jscript.Alert(this, "密码不正确,请重试!");
                return;
            }
            else
            {
                if (txtNewPwd.Text.Length != 0)
                {
                    Model.Sys_UserInfo model = new Model.Sys_UserInfo();
                    model.Loginid = info.Loginid;
                    model.password = Common.CommonHelper.MyMd5(txtNewPwd.Text);
                    if (new BLL.Sys_User().ExistPwdRecord(info.Loginid, model.password) == "1")
                    {
                        Jscript.Alert(this, "修改的密码不能与前5次相同!");
                    }
                    else
                    {
                        if (new BLL.Sys_User().updatePwd(model) == 1)
                        {
                            Jscript.Alert(this, "密码修改成功!");
                        }
                    }
                }
                else
                {
                    Jscript.Alert(this, "请输入新密码!");
                }
            }
         
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("login.aspx");
        }
    }
}
